package com.medical.controller;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.medical.HttpTools.HttpRequest;
import com.medical.service.HospinfoService;

/** 
* @author  LTT-AH-ZY
* @date 创建时间：2017年8月17日 上午10:19:39 
* @version 1.0 
* @parameter 参数及其意义
* @since 最早使用该方法/类/接口的JDK版本 
* @since  
* @return  
*/
@Controller
@RequestMapping(value="Main/NoneRegisterMain")
public class HospitalRegController {
	/**
	 * 注入hospinfoService服务
	 */
	@Autowired
	private HospinfoService hospinfoService;
	/**
	 * 初始化正式注册界面，包括下拉框的初始化
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/myhospitalReg")
	public ModelAndView myhospitalReg(HttpServletRequest request)throws Exception {
		HttpSession session = request.getSession();
		Integer hosploginid=(Integer)session.getAttribute("hosploginid");
		Map<String,Object> result = new HashMap<>();
		Map<String,Object> result2 = new HashMap<>();
		String code="";
		String msg="";
		try {
			JSONObject json = HttpRequest.sendPost("getinfo", "hosploginid="+hosploginid);
			code=json.get("code").toString();
			msg=json.get("msg").toString();
			System.out.println("输出这个json:"+json);
			if("100".equals(code)) {
				try {
					JSONObject data = json.getJSONObject("data");
					result.put("hospname", data.get("hospname").toString());
					result.put("hospgrade", data.get("hospgrade").toString());
					result.put("hospadrprovince", data.get("hospadrprovince").toString());
					result.put("hospadrcity", data.get("hospadrcity").toString());
					result.put("hospadrarea", data.get("hospadrarea").toString());
					result.put("hospabs", data.get("hospabs").toString());
					result.put("hospregidcard", data.get("hospregidcard").toString());
					result.put("hospfeature", data.get("hospfeature").toString());
					result.put("hospadrother", data.get("hospadrother").toString());
					result.put("hospphone", data.get("hospphone").toString());
					if(data.get("hosppraclicense").toString()!="") {
						String[] hosppraclicense=data.get("hosppraclicense").toString().split(",");
						result.put("hosppraclicense0", hosppraclicense[0].toString());
						try {
							result.put("hosppraclicense1", hosppraclicense[1].toString());
						} catch (Exception e) {
							result.put("hosppraclicense1", "");
						}
					}else {
						result.put("hosppraclicense0", "");
						result.put("hosppraclicense1", "");
					}
					if(data.get("hospregdocquacer").toString()!="") {
						String[] hospregdocquacer=data.get("hospregdocquacer").toString().split(",");
						result.put("hospregdocquacer0", hospregdocquacer[0].toString());
						try {
							result.put("hospregdocquacer1", hospregdocquacer[1].toString());
						} catch (Exception e) {
							result.put("hospregdocquacer1", "");
						}
					}else {
						result.put("hospregdocquacer0", "");
						result.put("hospregdocquacer1", "");
					}
					if(data.get("hosporgcodecer").toString()!="") {
						String[] hosporgcodecer=data.get("hosporgcodecer").toString().split(",");
						result.put("hosporgcodecer0", hosporgcodecer[0].toString());
						try {
							result.put("hosporgcodecer1", hosporgcodecer[1].toString());
						} catch (Exception e) {
							result.put("hosporgcodecer1", "");
						}
					}else {
						result.put("hosporgcodecer0", "");
						result.put("hosporgcodecer1", "");
					}
					if(data.get("hosplegalcer").toString()!="") {
						String[] hosplegalcer=data.get("hosplegalcer").toString().split(",");
						result.put("hosplegalcer0", hosplegalcer[0].toString());
						try {
							result.put("hosplegalcer1", hosplegalcer[1].toString());
						} catch (Exception e) {
							result.put("hosplegalcer1", "");
						}
					}else {
						result.put("hosplegalcer0", "");
						result.put("hosplegalcer1", "");
					}
				} catch (Exception e) {
					System.out.println("出错了，错误信息是："+e.getMessage());
					code="-300";
					msg="数据解析错误";
				}
			}
		} catch (Exception e) {
			System.out.println("出错了，错误信息是："+e.getMessage());
			code="-200";
			msg="数据传输错误";
		}
		try {
			JSONObject logininfojson = HttpRequest.sendPost("/getlogininfo","hosploginid="+hosploginid);
			System.out.println("输出这个logininfojson:"+logininfojson);
			String jsoncode = logininfojson.get("code").toString();
			String jsonmsg = logininfojson.get("msg").toString();
			if("100".equals(jsoncode)) {
				JSONObject datajson = logininfojson.getJSONObject("data");
				result2.put("hosplogintype", datajson.get("hosplogintype").toString());
				result2.put("hosplogintypename", datajson.get("hosplogintypename").toString());
			}
		} catch (Exception e) {
			System.out.println("1出错了，出错信息是："+e.getMessage());
		}
		System.out.println("输出这个result："+result);
		System.out.println("输出这个result2："+result2);
		//List<HosplevelCustom> hosplevelList = hospinfoService.InithosplevelList(null);
		List<Map<String,Object>> list = new ArrayList<>();
		try {
			JSONObject leveljson = HttpRequest.sendPost("listhosplevel", "");
			String jsoncode = leveljson.get("code").toString();
			String jsonmsg = leveljson.get("msg").toString();
			if("100".equals(jsoncode)) {
				try {
					JSONArray data = leveljson.getJSONArray("data");
					for(int i=0;i<data.length();i++) {
						Map<String,Object> map = new HashMap<>();
						JSONObject job = data.getJSONObject(i);
						map.put("hosplevelid", job.get("hosplevelid").toString());
						map.put("hosplevelname", job.get("hosplevelname").toString());
						list.add(map);
					}
				} catch (Exception e) {
					System.out.println("出错信息是："+e.getMessage());
				}
			}
		} catch (Exception e) {
			System.out.println("出错信息是："+e.getMessage());
		}
		ModelAndView model = new ModelAndView();
		//model.addObject("hosplevelList", hosplevelList);
		model.addObject("hospinfo", result);
		model.addObject("hosplogininfo", result2);
		model.addObject("hosplevelList", list);
		System.out.println();
		model.setViewName("MainFrame/NoneRegisterMain/MyHospitalReg");
		return model;
	}
	/**
	 * 个人信息提交类，详细个人信息注册
	 * @param request
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/myhospinforegSubmit")
	public ModelAndView myhospinforegSubmit(HttpServletRequest request,ModelAndView model)throws Exception{
		HttpSession session = request.getSession();
		Integer hosploginid = (Integer)session.getAttribute("hosploginid");
		System.out.println("输出这个参数："+request.getParameter("hospname"));
		String hospname = request.getParameter("hospname");
		String hospphone = request.getParameter("hospphone");
		String hospregidcard = request.getParameter("hospregidcard");
		String lng = request.getParameter("lng");String lat = request.getParameter("lat");
		String province = request.getParameter("province");String city = request.getParameter("city");
		String area = request.getParameter("area");String other = request.getParameter("other");
		String hosplevel = request.getParameter("hosplevel");
		String hospabs = request.getParameter("hospabs");String hospfeature = request.getParameter("hospfeature");
		
		String RegParam = "hosploginid="+hosploginid+"&hospname="+hospname+"&hospphone="+hospphone+"&hospregidcard="
						+hospregidcard+"&lng="+lng+"&lat="+lat+"&province="+province+"&city="
						+city+"&area="+area+"&other="+other+"&hosplevel="
						+hosplevel+"&hospabs="+hospabs+"&hospfeature="+hospfeature;
		System.out.println("输出这个param："+RegParam);
		try {
			JSONObject Regjson = HttpRequest.sendPost("updateinfo", RegParam);
			String jsoncode = Regjson.get("code").toString();
			String jsonmsg = Regjson.get("msg").toString();
			model.addObject("msg", jsonmsg);
			if("100".equals(jsoncode)) {
				model.addObject("info", "success");
			}else if("200".equals(jsoncode)){
				model.addObject("info", "warning");
			}else {
				model.addObject("info", "error");
			}
		} catch (Exception e) {
			model.addObject("info", "error");
		}
		Map<String,Object> result = new HashMap<>();
		Map<String,Object> result2 = new HashMap<>();
		String code="";
		String msg="";
		try {
			JSONObject json = HttpRequest.sendPost("getinfo", "hosploginid="+hosploginid);
			code=json.get("code").toString();
			msg=json.get("msg").toString();
			System.out.println("输出这个json:"+json);
			if("100".equals(code)) {
				try {
					JSONObject data = json.getJSONObject("data");
					result.put("hospname", data.get("hospname").toString());
					result.put("hospgrade", data.get("hospgrade").toString());
					result.put("hospadrprovince", data.get("hospadrprovince").toString());
					result.put("hospadrcity", data.get("hospadrcity").toString());
					result.put("hospadrarea", data.get("hospadrarea").toString());
					result.put("hospabs", data.get("hospabs").toString());
					result.put("hospregidcard", data.get("hospregidcard").toString());
					result.put("hospfeature", data.get("hospfeature").toString());
					result.put("hospadrother", data.get("hospadrother").toString());
					result.put("hospphone", data.get("hospphone").toString());
					if(data.get("hosppraclicense").toString()!="") {
						String[] hosppraclicense=data.get("hosppraclicense").toString().split(",");
						result.put("hosppraclicense0", hosppraclicense[0].toString());
						try {
							result.put("hosppraclicense1", hosppraclicense[1].toString());
						} catch (Exception e) {
							result.put("hosppraclicense1", "");
						}
					}else {
						result.put("hosppraclicense0", "");
						result.put("hosppraclicense1", "");
					}
					if(data.get("hospregdocquacer").toString()!="") {
						String[] hospregdocquacer=data.get("hospregdocquacer").toString().split(",");
						result.put("hospregdocquacer0", hospregdocquacer[0].toString());
						try {
							result.put("hospregdocquacer1", hospregdocquacer[1].toString());
						} catch (Exception e) {
							result.put("hospregdocquacer1", "");
						}
					}else {
						result.put("hospregdocquacer0", "");
						result.put("hospregdocquacer1", "");
					}
					if(data.get("hosporgcodecer").toString()!="") {
						String[] hosporgcodecer=data.get("hosporgcodecer").toString().split(",");
						result.put("hosporgcodecer0", hosporgcodecer[0].toString());
						try {
							result.put("hosporgcodecer1", hosporgcodecer[1].toString());
						} catch (Exception e) {
							result.put("hosporgcodecer1", "");
						}
					}else {
						result.put("hosporgcodecer0", "");
						result.put("hosporgcodecer1", "");
					}
					if(data.get("hosplegalcer").toString()!="") {
						String[] hosplegalcer=data.get("hosplegalcer").toString().split(",");
						result.put("hosplegalcer0", hosplegalcer[0].toString());
						try {
							result.put("hosplegalcer1", hosplegalcer[1].toString());
						} catch (Exception e) {
							result.put("hosplegalcer1", "");
						}
					}else {
						result.put("hosplegalcer0", "");
						result.put("hosplegalcer1", "");
					}
				} catch (Exception e) {
					System.out.println("出错了，错误信息是："+e.getMessage());
					code="-300";
					msg="数据解析错误";
				}
			}
		} catch (Exception e) {
			System.out.println("出错了，错误信息是："+e.getMessage());
			code="-200";
			msg="数据传输错误";
		}
		try {
			JSONObject logininfojson = HttpRequest.sendPost("/getlogininfo","hosploginid="+hosploginid);
			System.out.println("输出这个logininfojson:"+logininfojson);
			String jsoncode = logininfojson.get("code").toString();
			String jsonmsg = logininfojson.get("msg").toString();
			if("100".equals(jsoncode)) {
				JSONObject datajson = logininfojson.getJSONObject("data");
				result2.put("hosplogintype", datajson.get("hosplogintype").toString());
				result2.put("hosplogintypename", datajson.get("hosplogintypename").toString());
			}
		} catch (Exception e) {
			System.out.println("1出错了，出错信息是："+e.getMessage());
		}
		System.out.println("输出这个result："+result);
		System.out.println("输出这个result2："+result2);
		//List<HosplevelCustom> hosplevelList = hospinfoService.InithosplevelList(null);
		List<Map<String,Object>> list = new ArrayList<>();
		try {
			JSONObject leveljson = HttpRequest.sendPost("listhosplevel", "");
			String jsoncode = leveljson.get("code").toString();
			String jsonmsg = leveljson.get("msg").toString();
			if("100".equals(jsoncode)) {
				try {
					JSONArray data = leveljson.getJSONArray("data");
					for(int i=0;i<data.length();i++) {
						Map<String,Object> map = new HashMap<>();
						JSONObject job = data.getJSONObject(i);
						map.put("hosplevelid", job.get("hosplevelid").toString());
						map.put("hosplevelname", job.get("hosplevelname").toString());
						list.add(map);
					}
				} catch (Exception e) {
					System.out.println("出错信息是："+e.getMessage());
				}
			}
		} catch (Exception e) {
			System.out.println("出错信息是："+e.getMessage());
		}
		//model.addObject("hosplevelList", hosplevelList);
		model.addObject("hospinfo", result);
		model.addObject("hosplogininfo", result2);
		model.addObject("hosplevelList", list);
		model.setViewName("MainFrame/NoneRegisterMain/MyHospitalReg");
		return model;
	}
	/**
	 * 医院照片上传信息
	 * @param certify_pic
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value="/fileupload")
	public void fileupload(@RequestParam("certify_pic") MultipartFile certify_pic,HttpServletRequest request,HttpServletResponse response)throws Exception{
        //JSONObject json = HttpRequest.UpdatePic("updatepic", certify_pic);
        //System.out.println("输出这个json是："+json);
		HttpSession session = request.getSession();
		Integer hosploginid=(Integer)session.getAttribute("hosploginid");
        response.setCharacterEncoding("UTF-8");
        String flag="";String param="";
        String type=request.getParameter("type");
        String code="";
        String msg="";
		if(certify_pic!=null) {
			String name = certify_pic.getOriginalFilename();
	        System.out.println("输出这个name："+name);
	        String pic_path = "C:\\upload\\";
	        String fileName = certify_pic.getOriginalFilename();
	        String newFileName = UUID.randomUUID() + fileName.substring(fileName.lastIndexOf("."));
	        try {
	        	File newFile = new File(pic_path+newFileName);
	            param="hosploginid="+hosploginid+"&type="+type+"&picturefile="+pic_path+newFileName;
	            System.out.println("输出这个参数，param="+param);
	            certify_pic.transferTo(newFile);//将文件写到磁盘
	            try {
					JSONObject json = HttpRequest.sendPost("updatepicture", param);
					code=json.get("code").toString();
					msg=json.get("msg").toString();
					if("100".equals(code)) {
						flag="上传成功";
					}else {
						flag="上传失败："+msg;
					}
				} catch (Exception e) {
					flag="上传失败";
					System.out.println("数据传输错误");
				}
	            
			} catch (Exception e) {
				System.out.println("出错了，错误信息是："+e.getMessage());
				flag="上传失败";
			}
        }else {
        	flag="上传失败";
        }
		response.getWriter().print("{\"flag\":\""+flag+"\",\"code\":\""+flag+"\"}");
	}
	/**
	 * 提交审核资料
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/ExamineSubmit")
	public @ResponseBody Map<String,Object> ExamineSubmit(HttpServletRequest request)throws Exception{
		HttpSession session = request.getSession();
		Integer hosploginid=(Integer)session.getAttribute("hosploginid");
		String param = "hosploginid="+hosploginid;
		Map<String,Object> map = new HashMap<String,Object>();
		try {
			JSONObject json = HttpRequest.sendPost("/reviewinfo", param);
			String jsoncode=json.get("code").toString();
			String jsonmsg=json.get("msg").toString();
			
			map.put("code", jsoncode);
			map.put("msg", jsonmsg);
		} catch (Exception e) {
			System.out.println("出错了，出错信息是："+e.getMessage());
			
			map.put("code", "-100");
			map.put("msg", "数据传输错误！提交审核失败！");
		}
		return map;
	}
	
}
